<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            //使用对象计数
            // 定义变量
            const arr = [
                "衣服",
                "鞋子",
                "鞋子",
                "裤子",
                "裤子",
                "衣服",
                "衣服",
                "裤子",
            ];
            //定义空的对象
            let o = {};
            //循环遍历arr的数组内容
            for (let i = 0; i < arr.length; i++) {
                //把名字进行赋值给key
                let key = arr[i];
                // 判断数量
                if (o[key] === undefined) {
                    // 如果key是空的，让这个key下的值等于1
                    o[key] = 1;
                } else {
                    // 反之使他+1
                    o[key] += 1;
                }
            }
            console.log(o);
        </script>
    </body>
</html>
